/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Deal.PersonalInformation.Logic;

import Data.DAO.ICustomerStore;
import data.DAO.IOrderStore;
import data.pojo.Customer;
import data.pojo.Order;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author LiuBole
 */
//购买记录，即订单（Order）
public class OrderLogic {

    private ICustomerStore customerDS = new ICustomerStore();
    private IOrderStore orderDS = new IOrderStore();

    private String creatOrderID(Customer c){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        int n = c.getOrderList().size();
        return sdf.format(date) + n;
    }
    //将订单添加到OrderList.txt和顾客的List<Order>里
    public boolean addNewOrder(String customerID, double account, List<String[]> bookList,
            String name, String phone, String postcode, String address) {
        List<Customer> cuList = customerDS.getObjects();
        List<Order> orList = orderDS.getObjects();
        for(Customer c : cuList){
            if(c.getId().equals(customerID)){
                List<Order> orders = c.getOrderList();
                Order order = new Order();
                String orderID = creatOrderID(c);
                
                order.setOrderID(orderID);
                order.setCustomerID(customerID);
                order.setDealAmount(account);
                order.setBookList(bookList);
                
                order.setRecipients(name);
                order.setAddress(address);
                order.setPostcode(postcode);
                order.setPhone(phone);
                order.setStatus("刚下单");
                
                orders.add(order); //添加到customerList.txt
                customerDS.update(cuList);
                
                orList.add(order); //添加到orderList.txt
                orderDS.update(orList);
                return true;
            }
        }
        return false;
    }
    
    public Vector<List> orderTableVector(String customerID) {
        List<Customer> arr = customerDS.searchObjectsByAttr("id", customerID);
        if(arr.size()>0){
            Vector<Vector<String>> aVector = toCollectionTable(arr);
            if(aVector!=null)
                return new Vector<List>(aVector);
        }
        return null;
    }

    private Vector toCollectionTable(List<Customer> list) {
        try {
            List<Order> coList = list.get(0).getOrderList(); //seList中包含一个顾客
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (int i = 0; (coList != null) && (i < coList.size()); i++) {
                reList.addAll((Vector<Vector<String>>) toOrderStringArray(coList.get(i)));
            }
            return reList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**格式为：
     *记录1： ID  X  X  X * * * * * 
     *                    * * * * *
     *记录2： ID2 X  X  X * * * * *
     * 
     */
    private Vector toOrderStringArray(Order order) {
        Vector<Vector<String>> list = new Vector<>();
        Vector<String> str = new Vector<>();
        List<String[]> arrays = order.getBookList();
        
        str.add( order.getOrderID() );
        str.add( order.getCustomerID() );
        str.add( String.valueOf(order.getDealAmount()) );
        str.add( order.getStatus() );
        str.addAll( Arrays.asList(subArrays(arrays.get(0))) );
        list.add(str);
        
        for(int i=1; i<arrays.size(); i++){
            str = new Vector<>();
            str.addAll( Arrays.asList(new String[]{"", "", "", ""}) );
            str.addAll( Arrays.asList(subArrays(arrays.get(i))) );
            list.add(str);
        }
        return list;
    }
    
    private String[] subArrays(String[] str){
        String[] arr = new String[5];
        System.arraycopy(str, 0, arr, 0, 5);
        return arr;
    }
    
}